Skip to content

mcp: ignore the session ID on server/discover responses - #1523

Open
simon0191 wants to merge 1 commit into
modelcontextprotocol:mainfrom
simon0191:discover-session-id
Open

simon0191 wants to merge 1 commit into
modelcontextprotocol:mainfrom
simon0191:discover-session-id

Conversation

@simon0191

Copy link
Copy Markdown

A legacy server may reject server/discover with HTTP 200 and a JSON-RPC error while setting Mcp-Session-Id. The streamable client stored that ID and sent it on the fallback initialize, so a server that assigned a new session ID made Connect fail with mismatching session IDs.

server/discover is sessionless in 2026-07-28, and under 2025-11-25 the session ID belongs to the response carrying the InitializeResult, so the client no longer records a session ID from a server/discover response.

The new table test covers the HTTP 200 / -32601 case and four HTTP 400 rejection shapes seen from real legacy servers (plain text, -32600 with an unmatched id, -32600 with legacy-only data.supported), each with a session ID on the discover response.

Fixes #1522

AI disclosure: written with Claude Code; I reviewed the change and can explain it.

A legacy server may reject server/discover with HTTP 200 and a JSON-RPC
error while setting Mcp-Session-Id. The streamable client stored that ID
and sent it on the fallback initialize, so a server that assigned a new
session ID made Connect fail with "mismatching session IDs".

server/discover is sessionless in 2026-07-28, and under 2025-11-25 the
session ID belongs to the response carrying the InitializeResult, so the
client no longer records a session ID from a server/discover response.

Fixes modelcontextprotocol#1522

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Nov 12, 2026
@github-actions github-actions Bot closed this Nov 12, 2026
@github-actions github-actions Bot reopened this Nov 13, 2026
@github-actions github-actions Bot removed the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Nov 13, 2026
Comment thread mcp/streamable.go
Comment on lines 2668 to 2670
if hadSessionID == "" {
c.sessionID = sessionID
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if hadSessionID == "" && requestMethod == methodInitialize {
c.sessionID = sessionID
}

According to spec sessionId should be set up only during initialize

A server MAY assign a session ID "at initialization time". It does this by putting it in an MCP-Session-Id header on the HTTP response that carries the InitializeResult

Adding this check here should be more clean

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mcp: streamable client keeps the session ID from a rejected server/discover, so the fallback initialize fails

2 participants